package com.qf.ability.gateway.application;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.http.client.reactive.ReactorResourceFactory;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * author Ken
 * create_time 2022/11/24
 */
@SpringBootApplication
public class GatewayApplication {

    public static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class, args);
    }

    @Bean
    public ReactorResourceFactory reactorClientResourceFactory() {
        //Netty底层是主从线程池模式
        //设置主线程池的线程数
        System.setProperty("reactor.netty.ioSelectCount","1");

        //设置从线程池（工作线程池）的线程数 默认就是4个
        //Runtime.getRuntime().availableProcessors() - 获取当前电脑的CPU核心数
        int ioWorkerCount = Math.max(Runtime.getRuntime().availableProcessors() * 3, 4);
        System.setProperty("reactor.netty.ioWorkerCount",String.valueOf(ioWorkerCount));
        return new ReactorResourceFactory();
    }



    /**
     * Gateway 网关解决跨域问题
     * @return
     */
    @Bean
    public CorsWebFilter getCorsWebFilter(){

        CorsConfiguration configuration = new CorsConfiguration();
        configuration.addAllowedMethod("*");
        configuration.addAllowedOrigin("*");
        configuration.addAllowedHeader("*");
        configuration.setAllowCredentials(true);

        UrlBasedCorsConfigurationSource corsConfigurationSource = new UrlBasedCorsConfigurationSource();
        corsConfigurationSource.registerCorsConfiguration("/**", configuration);
        return new CorsWebFilter(corsConfigurationSource);
    }

}
